/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.cardapiodigital.data;

import br.com.cardapiodigital.database.ConnectionManager;
import br.com.cardapiodigital.entity.Destaques;
import br.com.cardapiodigital.entity.Estabelecimentos;
import br.com.cardapiodigital.exception.CardapioDigitalException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author egberto
 */
public class DestaquesData extends GenericData<Destaques> {
     
    
    @Override
    public void salvar(Destaques entity) throws CardapioDigitalException {
        PreparedStatement pstmt = null;
        Connection conn = null;
        
        String sql = "INSERT INTO destaques(datainicial,datafinal,link,estabelecimentos_id)" +
                "values (?,?,?,?)";    
        
        try{
            conn = ConnectionManager.getConexao();
            pstmt = conn.prepareStatement(sql);
            // preenche os valores ?,?                       
            
            pstmt.setDate(1,new java.sql.Date(entity.getDataincial().getTime()));
            pstmt.setDate(2,new java.sql.Date(entity.getDatafinal().getTime()));           
            pstmt.setString(3, entity.getLink());            
            pstmt.setInt(4, entity.getEstabelecimentosId().getId());
            
            pstmt.execute();
            
                       
        }catch(SQLException ex){
            Logger.getLogger(DestaquesData.class.getName()).
                    log(Level.SEVERE, null, ex);
            String errorMsg = "Erro na execucao da query" + sql;
            throw new CardapioDigitalException(errorMsg, ex);
        }finally{
            ConnectionManager.close(conn, pstmt);
        }
    }
  
    @Override
    public void atualizar(Destaques entity) throws CardapioDigitalException {
        PreparedStatement pstmt = null;
        Connection conn = null;
        
        String sql = "UPDATE destaques SET datainicial = ?," +
                     "datafinal = ?, link = ?, estabelecimentos_id = ? " +
                     "WHERE id = ?";    
        
        try{
            conn = ConnectionManager.getConexao(); 
            pstmt = conn.prepareStatement(sql);
            // preenche os valores ?,?
            pstmt.setDate(1,new java.sql.Date(entity.getDataincial().getTime()));
            pstmt.setDate(2,new java.sql.Date(entity.getDatafinal().getTime()));           
            pstmt.setString(3, entity.getLink());            
            pstmt.setInt(4, entity.getEstabelecimentosId().getId());             
            pstmt.setInt(5, entity.getId());
            
            pstmt.execute();
            
        }catch(SQLException ex){
            Logger.getLogger(DestaquesData.class.getName()).
                    log(Level.SEVERE, null, ex);
            String errorMsg = "Erro na execucao da query" + sql;
            throw new CardapioDigitalException(errorMsg, ex);
        }finally{
            ConnectionManager.close(conn, pstmt);
        }
    }

    
    @Override
    public List<Destaques> buscarTodos() throws CardapioDigitalException {
        String sql = "SELECT * FROM destaques ORDER BY estabelecimentos_id,datafinal ASC";
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;
        List<Destaques> listaDestaques = new ArrayList<Destaques>();
        
        try{
            conn = ConnectionManager.getConexao();
            stmt =  conn.createStatement();
            rs = stmt.executeQuery(sql);
            
            while(rs.next()){
                int id = rs.getInt("id");
                Date datainicial = rs.getDate("datainicial");
                Date datafinal = rs.getDate("datafinal");
                String link = rs.getString("link");
                int estabelecimento = rs.getInt("estabelecimentos_id");
                
                listaDestaques.add(new Destaques(id, datainicial, datafinal, link, 
                        (new EstabelecimentosData().obtem(estabelecimento))));
            }
            
            return listaDestaques;
        
        }catch(SQLException ex){
            Logger.getLogger(DestaquesData.class.getName()).
                    log(Level.SEVERE, null, ex);
            String errorMsg = "Erro na execucao da query" + sql;
            throw new CardapioDigitalException(errorMsg, ex);
        }finally{
            ConnectionManager.close(conn,stmt,rs);
        }
    }

    @Override
    public Destaques obtem(int entityId) throws CardapioDigitalException {
        String sql = "SELECT * FROM destaques WHERE id='" + entityId + "'";        
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;
        Destaques destaques = null;
        
        try{
            conn = ConnectionManager.getConexao();
            stmt =  conn.createStatement();  
            rs = stmt.executeQuery(sql);
            
            if(rs.next()){
                int id = rs.getInt("id");
                Date datainicial = rs.getDate("datainicial");
                Date datafinal = rs.getDate("datafinal");
                String link = rs.getString("link");
                int estabelecimento = rs.getInt("estabelecimentos_id");
                
                destaques = new Destaques(id, datainicial, datafinal, link, 
                        (new EstabelecimentosData().obtem(estabelecimento)));
            }
            
            return destaques;
        
        }catch(SQLException ex){
            Logger.getLogger(DestaquesData.class.getName()).
                    log(Level.SEVERE, null, ex);
            String errorMsg = "Erro na execucao da query " + sql;
            throw new CardapioDigitalException(errorMsg, ex);
        }finally{
            ConnectionManager.close(conn,stmt,rs);
        }
    }
    
    
    
}
